<?php
$key = $_GET['key'];

$res = file_get_contents('php://input');
$data = json_decode($res, true);
$version = $data['version'];
$file = $data['file'];
$date = $data['date'];
$debug = $data['debug'];
$update = $data['update'];

$Version_f = fopen('./'.$key.'/'.$key.'-v'.$version.'.zip', "wb");
fwrite($Version_f, base64_decode($file));
fclose($Version_f);

$index_by_string = file_get_contents('./index.json');
$index_by = json_decode($index_by_string, true);
$count = count($index_by);
for($i=0;$i<$count;$i++){
    if ($index_by[$i]['key'] == $key) {
        $index_by[$i]['lastVersion'] = $version;
        break;
    }
};
$last_path = fopen('./index.json', "wb");
fwrite($last_path, json_encode($index_by));
fclose($last_path);


$root_string = file_get_contents('./'.$key.'/index.json');
$root_by = json_decode($root_string, true);
$root_by_config = [
    "date"=>$date,
    "debug"=>$debug,
    "update"=>$update,
    "FileName"=>$key.'-v'.$version.'.zip',
    "version"=>$version,
];
array_unshift($root_by,$root_by_config);
$root_Path_Save = fopen('./'.$key.'/index.json', "wb");
fwrite($root_Path_Save, json_encode($root_by));
fclose($root_Path_Save);



echo true;


?>